﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Maticsoft.Common;

namespace Admin.Admin.AdminMan
{
    public partial class AdminModel : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            IsLogin();

            //验证是不是超级管理员，只有超级管理员才有权利访问页面
            if (!this.isSuperAdmin)
            {
                Maticsoft.Common.MessageBox.ShowAndRedirect(this, "您暂时没有权利访问该页面", "../index.aspx");
                return;
            }

            if (!IsPostBack)
            {
                if (Request.QueryString["AdminID"] != null)
                {
                    int adminID = Convert.ToInt32(Request.QueryString["AdminID"]);
                    Bind(adminID);
                }
            }
        }

        protected void Bind(int adminID)
        {
            NS.Model.Admins adminModel = new NS.BLL.Admins().GetModel(adminID);
            this.AdminID.Text = adminModel.AdminID.ToString();
            this.AdminAccount.Text = adminModel.Account;
            this.Password.Text =adminModel.Password;
        }

        protected void Editor_Click(object sender, EventArgs e)
        {
            string adminAccount = this.AdminAccount.Text.Trim();
            string password = this.Password.Text.Trim();

            #region 数据验证
            //验证重复
            if (password == "")
            {
                MessageBox.Show(this, "密码不能为空!");
                return;
            }

            #endregion

            int adminID = Convert.ToInt32(this.AdminID.Text);
            NS.BLL.Admins adminBll = new NS.BLL.Admins();
            NS.Model.Admins adminModel = new NS.Model.Admins();
            adminModel = adminBll.GetModel(adminID);
            adminModel.Account = adminAccount;
            adminModel.Password =password;
            adminBll.Update(adminModel);
            MessageBox.ShowAndRedirect(this, "更新成功！", "AdminList.aspx");
        }

        protected void Delete_Click(object sender, EventArgs e)
        {
            NS.BLL.Admins adminBll = new NS.BLL.Admins();
            int adminID = int.Parse(this.AdminID.Text);
            if (adminBll.Delete(adminID))
            {
                Maticsoft.Common.MessageBox.ShowAndRedirect(this, "删除成功", "AdminList.aspx");
            }
            else
            {
                Maticsoft.Common.MessageBox.Show(this, "删除失败");
            }
        }
    }
}